package controllers

import (
	"quickstart/models"

	"github.com/astaxie/beego"
)

type ArticleControllers struct {
	beego.Controller
}

//文章列表渲染模板
func (c *ArticleControllers) Get() {
	data := models.GetArticleList()
	c.Data["article"] = &data
	c.TplName = "article_list.html"
}

//获取单个文章数据
func (c *ArticleControllers) ArticleById() {
	id, _ := c.GetInt("id")
	data := models.GetArticleById(id)
	c.Data["json"] = &data
	c.ServeJSON()
}

//新增文章数据
func (c *ArticleControllers) AddArticle() {
	title := c.GetString("title")
	content := c.GetString("content")
	data := models.AddArticleData(title, content)
	c.Data["json"] = &data
	c.ServeJSON()
}

//更改文章数据
func (c *ArticleControllers) SetArticle() {
	id, _ := c.GetInt("id")
	title := c.GetString("title")
	content := c.GetString("content")
	err := models.UpdateArticle(title, content, id)
	c.Data["err"] = err
	c.ServeJSON()
}

//删除单条文章数据
func (c *ArticleControllers) DelArticle() {
	id, _ := c.GetInt("id")
	data, err := models.DelArticleById(id)
	c.Data["json"] = &data
	c.Data["err"] = err
	c.ServeJSON()
}
